package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.service.ReturnListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/returnListGoods")
public class returnListController {
    @Autowired
    private ReturnListService returnListService;
    //保存退货单
    @PostMapping("save")
    public ServiceVO<ReturnList> saveReturnList(ReturnList returnList, String returnListGoodsStr, HttpServletRequest request){
        String returnNumber = request.getParameter("returnNumber");
        ServiceVO<ReturnList> serviceVO = returnListService.saveReturnListAndGoods(returnList,returnNumber,returnListGoodsStr);
        return serviceVO;
    }
    //退货单列表展示（可条件查询：单据号模糊、供应商、是否付款和日期区间）
    //退货单 下拉列表
    @PostMapping("list")
    public Map<String,Object> getReturnList(String returnNumber,Integer supplierId, Integer state,String sTime,String eTime){
        HashMap<String, Object> map = new HashMap<>();
       List<ReturnList> list = returnListService.getReturnList(returnNumber,supplierId,state,sTime,eTime);
        map.put("rows",list);
        return map;
    }
    //4.2、显示退货单商品信息
    @PostMapping("goodsList")
    public Map<String,Object> getReturnListGoodsList(Integer returnListId){
        HashMap<String, Object> map = new HashMap<>();
        List<ReturnListGoods> list = returnListService.getReturnListGoodsList(returnListId);
        map.put("rows",list);
        return map;
    }
    //4.3、删除退货单 并删除退货单对应商品
    @PostMapping("delete")
    public ServiceVO delectReturnListAndGoods(Integer returnListId){
        ServiceVO serviceVO = returnListService.delectReturnListAndGoods(returnListId);
        return serviceVO;
    }
    //3.2、退货统计（可根据 商品类别、商品编码或名称 条件查询）
    @PostMapping("count")
    public String countReturnListGoods(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        String jsonStr = returnListService.countReturnListGoods(sTime,eTime,goodsTypeId,codeOrName);
        return jsonStr;
    }
    //http://localhost:8080/returnListGoods/updateState
    //returnListId: 6    ServiceVO
    //结算退货单
    @PostMapping("updateState")
    public ServiceVO updateStateReturnList(String returnListId){
        ServiceVO serviceVO = returnListService.updateStateReturnList(returnListId);
        return serviceVO;
    }
}

